package dialogs;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import custom.MyDialog;

/**
 * Created by Administrator on 2018/5/17.
 */

public class StorageWarningModifyDialog extends MyDialog {

    @BindView(R.id.popwindow_jian)
    LinearLayout popwindowJian;
    @BindView(R.id.et_popwindow_number)
    EditText etNumber;
    @BindView(R.id.popwindow_jia)
    LinearLayout popwindowJia;
    @BindView(R.id.bt_popwindow_cancel)
    Button btPopwindowCancel;
    @BindView(R.id.bt_popwindow_modify)
    Button btPopwindowModify;
    @BindView(R.id.tv_dialog_title)
    TextView tvDialogTitle;
    private ModifyInterface listener;
    private String number;
    private String title;

    public StorageWarningModifyDialog(@NonNull Context context, int themeResId,String title, String number, ModifyInterface listener) {
        super(context, themeResId);
        this.listener = listener;
        this.number = number;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_popwindow);
        ButterKnife.bind(this);
        tvDialogTitle.setText(title);
        etNumber.setText(number);
        etNumber.setSelection(number.length());
    }

    @OnClick({R.id.popwindow_jian, R.id.popwindow_jia, R.id.bt_popwindow_cancel, R.id.bt_popwindow_modify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.popwindow_jian:
                String num = etNumber.getText().toString();
                if (Integer.parseInt(num) > 0) {
                    num = Integer.parseInt(num) - 1 + "";
                    etNumber.setText(num);
                    etNumber.setSelection(num.length());
                } else {
                    etNumber.setText("0");
                    etNumber.setSelection(num.length());
                }
                break;
            case R.id.popwindow_jia:
                String n = etNumber.getText().toString().trim();
                if (Integer.parseInt(n) < 9999) {
                    n = Integer.parseInt(n) + 1 + "";
                    etNumber.setText(n);
                    etNumber.setSelection(n.length());
                }
                break;
            case R.id.bt_popwindow_cancel:
                dismiss();
                break;
            case R.id.bt_popwindow_modify:
                String count = etNumber.getText().toString().trim();
                if (!count.equals(number)) {
                    for (int i = 0; i < count.length(); i++) {
                        if (count.startsWith("0")) {
                            if (count.length() != 1) {
                                count = count.substring(1);
                            }
                        }
                    }
                    if (TextUtils.isEmpty(count)) {
                        count = "0";
                    }
                    listener.modify(count);
                }
                dismiss();
                break;
        }
    }

    public interface ModifyInterface {
        void modify(String number);
    }
}
